<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Messages;
/*消息通知*/
class Message extends Common
{
  //消息通知列表接口
  public  function newsList(){
         $uid = Request::instance()->post('uid','');//用户id
         $page = Request::instance()->post('page','');//页码值
        if(empty($uid)){
          return json(['state'=>'203','msg'=>'用户为空请登录']);
        }
        if(empty($page)){
          return json(['state'=>'204','msg'=>'页码值为空']);
        }
       $rs=Messages::userSel($uid);
       if($rs){
        $limit=5;
        $start=($page-1)*$limit;
          $concernId=$rs['concernId'];//关注状态通知 1通知 -1不通知
          $actnotify=$rs['actnotify'];//关注的人新活动通知 1通知 -1不通知
          $captain_state=$rs['captain_state'];//关注的领队新活动通知 1通知 -1不通知
          $where="uid=$uid";
          if($concernId=='1'){
            //查询删除的数据
           $newsList=Db::name('user_message_record')->where(['uid'=>$uid,'genreId'=>'1'])->field('news_id')->select();
           if($newsList){
            $news=array_column($newsList,'news_id');
             $news_id=implode(',',$news);
           }else{
            $news_id="";
             }
           //查询关注我的用户
           $inviteList=Db::name('user_message')->where(['peerId'=>$uid,'genreId'=>'1'])->where('id','not in',$news_id)->field('id as news_id,uid as nearby_id,genreId,nickname,userPhoto,create_time')->select();
           if(empty($inviteList)){
            $inviteList=array();
           }
           
          }
          //关注的人新活动通知 1通知 -1不通知
          if($actnotify=='1'){
             //查询删除的数据
           $newsList=Db::name('user_message_record')->where(['uid'=>$uid,'genreId'=>'2'])->field('news_id')->select();
           if($newsList){
            $news=array_column($newsList,'news_id');
             $news_id=implode(',',$news);
           }else{
            $news_id="";
             }
           //查询我关注的用户
           $relation=Db::name('user_relation')->where(['fans_uid'=>$uid])->field('uid as nearby_id')->select();
           if(!empty($relation)){
             $rela=array_column($relation,'nearby_id');
             $rela_id=implode(',',$rela);
             $beinviteList=Db::name('user_message')->where('uid','in',$rela_id)
                          ->where(['genreId'=>'2'])
                          ->where('id','not in',$news_id)
                          ->field('id as news_id,uid as nearby_id,genreId,nickname,userPhoto,activity_id,create_time')->select();
           }else{
            $beinviteList=array();
           }
          }
          // print_r($beinviteList);die;
          if($captain_state=='1'){
            //查询删除的数据
           $newsList=Db::name('user_message_record')->where(['uid'=>$uid,'genreId'=>'3'])->field('news_id')->select();
           if($newsList){
            $news=array_column($newsList,'news_id');
             $news_id=implode(',',$news);
           }else{
            $news_id="";
             }
           //查询关注的领队
           $relation=Db::name('user_captain')->where(['uid'=>$uid])->field('captain_id as nearby_id')->select();
           //print_r($relation);die;
           if(!empty($relation)){
             $rela=array_column($relation,'nearby_id');
             $rela_id=implode(',',$rela);
             $captionList=Db::name('user_message')->where('uid','in',$rela_id)
                    ->where(['genreId'=>'3'])
                     ->where('id','not in',$news_id)
                    ->field('id as news_id,uid as nearby_id,genreId,nickname,userPhoto,activity_id,create_time')->select();
           }else{
            $captionList=array();
            }
           }
          $shop=array_merge($inviteList,$beinviteList,$captionList);
           $juli_sort = array();
            foreach ($shop as $arr2) {
              $juli_sort[] = $arr2['create_time'];
            }
            array_multisort($juli_sort, SORT_DESC,$shop);
           $result = array_slice($shop,$start,$limit);
           //添加我的查看时间
           $mapdata=['uid'=>$uid,'create_time'=>time()];
           Db::name('user_query')->insert($mapdata);
           if($result){
          return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
          }else{
          return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
          }
       }else{
        return json(['state'=>'202','msg'=>'用户已不存在']);
       }
    }
    //消息删除记录接口
    public function newsDel(){
      $uid=input('uid');//获取用户id
      $news_id=input('news_id');//获取消息主键id
      $genreId=input('genreId');//获取消息类型 1关注状态通知 2关注的人新活动通知 3关注的领队新活动通知
      if(empty($uid) || empty($news_id) || empty($genreId)){
        return json(['state'=>'202','msg'=>'参数不足']);
      }
      $data=['uid'=>$uid,'news_id'=>$news_id,'genreId'=>$genreId,'create_time'=>date("Y-m-d H:i:s")];
      $result=Db::name('user_message_record')->insert($data);
      if($result){
        return json(['state'=>'200','msg'=>'删除成功']);
      }else{
        return json(['state'=>'201','msg'=>'删除失败再试试']);
      }
    }
    //查询最新的消息
    public function news_message(){
      $uid=input('uid');
      if(empty($uid)){
        return json(['state'=>'202','msg'=>'用户为空']);
      }
       $rs=Messages::userSel($uid);

       if($rs){
        $oldTime= Db::name('user_query')->where(['uid'=>$uid])->value('create_time');
          $concernId=$rs['concernId'];//关注状态通知 1通知 -1不通知
          if($concernId=='1'){
             //查询删除的数据
           $newsList=Db::name('user_message_record')
             ->where(['uid'=>$uid,'genreId'=>'1'])
             ->field('news_id')->select();
           if($newsList){
            $news=array_column($newsList,'news_id');
             $news_id=implode(',',$news);
           }else{
            $news_id="";
             }
           //查询关注我的人
            $news1=Db::name('user_message')->where(['peerId'=>$uid,'genreId'=>'1'])->where('id','not in',$news_id)->where('create_time', '>','$oldTime')->field('id as news_id,uid as nearby_id,nickname,userPhoto,create_time,genreId')->select(); 
           
            if(empty($news1)){
              $news1=array();
            }
          }
          $actnotify=$rs['actnotify'];//关注的人新活动通知 1通知 -1不通知
           if($actnotify=='1'){
             //查询删除的数据
           $newsList=Db::name('user_message_record')
             ->where(['uid'=>$uid,'genreId'=>'2'])
             ->field('news_id')->select();
           if($newsList){
            $news=array_column($newsList,'news_id');
             $news_id=implode(',',$news);
           }else{
            $news_id="";
             }
           //查询我关注的人参加的新活动
           $nearbyId=Db::name('user_relation')->where(['fans_uid'=>$uid])->column('uid');
            $nearby_id=implode(",", $nearbyId);
            $news2=Db::name('user_message')->where('uid','in',$nearby_id)
                          ->where('create_time', '>','$oldTime')
                          ->where('id','not in',$news_id)
                          ->where(['genreId'=>'2'])
                          ->field('id as news_id,uid as nearby_id,nickname,userPhoto,create_time,genreId')
                          ->select(); 
                         
            if(empty($news2)){
              $news2=array();
            }
          }
          $captain_state=$rs['captain_state'];//关注的领队新活动通知 1通知 -1不通知
           if($captain_state=='1'){
             //查询删除的数据
           $newsList=Db::name('user_message_record')
             ->where(['uid'=>$uid,'genreId'=>'3'])
             ->field('news_id')->select();
           if($newsList){
            $news=array_column($newsList,'news_id');
             $news_id=implode(',',$news);
           }else{
            $news_id="";
             }
           //查询我关注的参加的发布的新活动
           $captainId=Db::name('user_captain')->where(['uid'=>$uid])->column('captain_id');
            $captain_id=implode(",", $captainId);
            $news3=Db::name('user_message')->where('uid','in',$captain_id)
                       ->where('id','not in',$news_id) 
                       ->field('id as news_id,uid as nearby_id,nickname,userPhoto,create_time,genreId')
                       ->where('create_time', '>','$oldTime')->select(); 
            if(empty($news3)){
              $news3=array();
            }
          }
           $shop=array_merge($news1,$news2,$news3);
          
           $num=count($shop);
           if(!empty($shop)){
            //降序序排列，按照“create_time”字段排列
            $juli_sort = array();
            foreach ($shop as $arr2) {
              $juli_sort[] = $arr2['create_time'];
            }
            array_multisort($juli_sort, SORT_DESC,$shop);
           }
           //创建数组
           //$result=array();
           if($shop){
           	$result=$shop[0];
           	$result['number']=$num;
            return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
            }else{
            $result['number']=$num;
            return json(['state'=>'200','msg'=>'查询成功','result'=>$result]);
          }
          }else{
            return json(['state'=>'201','msg'=>'用户已不存在']);
          }
        }
 
}